package cn.wsk;

import cn.wsk.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName demo
 * @Description TODO
 * @Author 王帅坤
 * @Date 2021/4/10 0010 上午 11:34
 * @Version 1.0
 **/
public class demo {
    public static void main(String[] args) throws IOException {
        // 指定配置文件的位置
        String resource = "mybatis-config.xml";

        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //构建sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象
        try (SqlSession session = sqlSessionFactory.openSession()) {

            // 对SqlSession进行数据库操作
            User user = (User) session.selectOne("abc.selectById", 1);

            System.out.println(user);

            int count = (int) session.selectOne("abc.selectCount");
            System.out.println(count);

            User user1 = (User) session.selectOne("abc.selectByName", "李明");
            System.out.println(user1);


        }


    }
}
